package com.kou.backend.service;

import com.kou.backend.dto.user.StoreFileResultDTO;
import com.kou.backend.entity.FileResourceDO;
import com.kou.backend.request.file.UploadFileRequestDTO;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * 文件存储服务
 *
 * @author: kou
 * @since: 2025/6/26 17:21
 */
public interface FileStorageService {
    /**
     * 存储文件，返回存储路径或访问URL
     */
    StoreFileResultDTO storeFile(MultipartFile file, UploadFileRequestDTO requestDTO, String uploader) throws IOException;

    /**
     * 读取文件流
     */
    InputStream getFileStream(String fileId) throws IOException;

    /**
     * 读取文件流（用于预览或下载）
     */
    void fileStreamToResponse(String fileId, String type, HttpServletResponse response) throws IOException;

    /**
     * 生成预览或下载URL
     *
     * @param fileId
     * @param expireSeconds
     * @return
     * @throws IOException
     */
    String generatePresignedUrl(String fileId, Integer expireSeconds) throws IOException;

    /**
     * 删除文件
     */
    void deleteFile(String userAccount, String uploader) throws IOException;

    /**
     * 保存文件
     */
    void saveFile(MultipartFile file, UploadFileRequestDTO requestDTO, String relativePath, String uploader);

    /**
     * 保存错误时删除的补偿信息
     *
     * @param storeFileResultDTO 存储文件结果
     */
    void saveErrorDeleteCompensateFileDbInfo(StoreFileResultDTO storeFileResultDTO);

    /**
     * 检查文件是否存在
     *
     * @param fileMd5
     * @return
     */
    boolean checkFileExists(String fileMd5);

    /**
     * 获取已上传的分片
     *
     * @param fileMd5
     * @return
     */
    List<Integer> getUploadedChunks(String fileMd5);

    /**
     * 上传分片
     *
     * @param fileMd5     文件MD5
     * @param chunkNumber 分片编号
     * @param chunkFile   分片文件
     * @param uploader    上传者
     * @throws IOException
     */
    void uploadChunk(String fileMd5, Integer chunkNumber, MultipartFile chunkFile, String uploader) throws IOException;

    /**
     * 合并分片
     *
     * @param fileMd5
     * @param uploader
     * @param originalFileName
     * @return
     * @throws IOException
     */
    String mergeChunks(String fileMd5, String uploader, String originalFileName) throws IOException;

    FileResourceDO getFileResourceDO(String fileId);
}
